package com.ncloud.timetask.dashboard.mapper;

import org.apache.ibatis.annotations.*;

/**
 * Created by shiqiang on 2017/7/6.
 */
@Mapper
public interface MoveHistoryMapper {
    /*订单历史处理begin*/
    @Insert(value = "insert into bs_oh_order(order_id,cust_id,cust_name,order_type,order_state,create_date,exec_time,prod_id," +
            "finish_time,channel_id,create_user,remark)" +
            " select order_id,cust_id,cust_name,order_type,order_state,create_date,exec_time,prod_id," +
            "finish_time,channel_id,create_user,remark from bs_o_order where order_id = #{order_id}")
    public int insertHisOrder(Long orderId);

    @Delete("delete from bs_o_order where order_id =#{orderId}")
    public int deleteByOrderId(Long orderId);

    /*订单历史处理begin*/
    @Insert(value = "insert into bs_oh_work(work_id,node_id, construct_item, construct_user,"
            + " construct_time, remark, finish_time, order_id, work_state)"
            + " select work_id, node_id, construct_item, construct_user, construct_time,"
            + " remark, finish_time, order_id, work_state from bs_o_work where order_id = #{order_id}")
    public int insertHisWork(Long orderId);

    @Delete("delete from bs_o_work where order_id = #{orderId}")
    public int deleteHisWork(Long orderId);

    /*判断是否可以搬入历史条件*/
    //select count(1) from bs_o_work a where  EXISTS (select work_id from bs_o_work b where b.order_id =a.order_id and a.work_state=b.work_state and b.work_state<>'3') and a.order_id =201707041710480647
    @Select(value = ("select count(1) from bs_o_work a where  EXISTS (select work_id from bs_o_work b where " +
            " b.order_id =a.order_id and a.work_state=b.work_state and b.work_state<>#{0}) and a.order_id =#{1}"))
    public Integer getNotIsCurrentState(String workState,Long orderId);

    @Update("update bs_o_work a set a.work_state='9' where order_id=#{orderId}")
    int updateWorkStateHis(Long orderId);
}
